//
//  PopPluginTableView.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/11/23.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "PopPluginTableView.h"
#import "AppDelegate.h"
#import "SubjectMessage.h"
#import "SessonContentMessage.h"
#import "MainChatController.h"
#import "MainChatView.h"

@interface PopPluginTableView()

@end

@implementation PopPluginTableView

- (id)initWithFrame:(CGRect)frame type:(RequestDataModelType)type didSelectedBlock:(DidSelectedBlock)didSelectedBlock{
    
    if (self = [super initWithFrame:frame]) {
        
        _type = type;
        _didSelectedBlock = didSelectedBlock;
        [self addSubview:self.tableView];
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = kGetColor(18, 38, 74);
        
        if (_type == SubjectDataModelType) {
            self.dataListOne = kAppDelegate.subjectList;
        } else {
            
            NSIndexPath *curIndexPath = kAppDelegate.rootVC.curIndexPath;
            NSMutableArray *subjectList = [kAppDelegate.sessionContentList objectAtIndex:curIndexPath.section];
            self.dataListOne = [[NSMutableArray alloc] init];
            
            if (subjectList && subjectList.count > 0) {
                for (NSMutableArray *sessionList in subjectList) {
                    if (sessionList && sessionList.count > 0) {
                        [self.dataListOne addObject:sessionList.lastObject];
                    }
                }
            }
            
        }
        [self reloadData];
        
    }
    
    return self;
}

- (void)reloadData {
    
    
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.delaysContentTouches = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setSeparatorInset:UIEdgeInsetsZero];
        tableView.contentOffset = CGPointMake(0, 0);
        _tableView = tableView;
    }
    
    return _tableView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataListOne count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *curIndexPath = kAppDelegate.rootVC.curIndexPath;

    if (_type == SubjectDataModelType) {
        
        SubjectMessage *subjectMessage = (SubjectMessage *)[self.dataListOne objectAtIndex:indexPath.row];
        
        static NSString *cellId = @"SubjectMessageIdentifier";;
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.backgroundColor = kGetColor(18, 38, 74);
        }
        
        cell.textLabel.text = subjectMessage.content;
        if (curIndexPath.section == indexPath.row) {
            cell.textLabel.textColor = [UIColor greenColor];
        } else {
            cell.textLabel.textColor = [UIColor whiteColor];
        }
        
        return cell;
    } else {
        
        SessonContentMessage *sessonContentMessage = (SessonContentMessage *)[self.dataListOne objectAtIndex:indexPath.row];
        
        static NSString *cellId = @"SubjectMessageIdentifier";;
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.backgroundColor = kGetColor(18, 38, 74);
        }
        
        cell.textLabel.text = sessonContentMessage.msg_content;
        if (curIndexPath.row == indexPath.row) {
            cell.textLabel.textColor = [UIColor greenColor];
        } else {
            cell.textLabel.textColor = [UIColor whiteColor];
        }

        return cell;

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSIndexPath *curIndexPath = kAppDelegate.rootVC.curIndexPath;

    if (_type == SubjectDataModelType) {

        NSInteger row = curIndexPath.row;
        NSInteger curRow = [(NSMutableArray *)[kAppDelegate.sessionContentList objectAtIndex:indexPath.row] count];
        if (row >= curRow) {
            row = curRow - 1;
        }

        [kAppDelegate.rootVC.sv setCurIndexPath:[NSIndexPath indexPathForRow:row inSection:indexPath.row] openAnimator:NO];
    
    } else {
        
        [kAppDelegate.rootVC.sv setCurIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:curIndexPath.section] openAnimator:NO];
    }
    
    if (_didSelectedBlock) {
        _didSelectedBlock();
    }
}

@end
